package com.brucekong.slm.controller.user;

import com.brucekong.slm.common.PageResult;
import com.brucekong.slm.common.Result;
import com.brucekong.slm.model.user.UserDomain;
import com.brucekong.slm.service.user.UserService;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.util.StringUtil;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 用户控制类
 */
@RestController
@RequestMapping("/user")
@Slf4j
@Api(value = "用户模块", tags = "用户模块")
public class UserController {

    @Autowired
    private UserService userService;
    // 不戴分页信息的
    private Result result = new Result();
    // 带分页信息的
    private PageResult pageResult = new PageResult();

    @GetMapping("/judge")
    @SuppressWarnings("unchecked")
    @ApiOperation(value = "是否存在用户", notes = "是否存在该用户", code = 200, produces = "application/json", responseContainer = "Map", response = Result.class)
    @ApiImplicitParam(value = "用户账号", name = "userName", required = true, dataType = "string", paramType = "query")
    public Result judgeUsername(String userName) {
        log.info("/user/judge");
        UserDomain userDomain = userService.getUserWithoutPasswordByUserName(userName);
        Map<String, Object> map = new HashMap<>();
        if (userDomain != null) {
            map.put("status", true);
            result.setCode(0);
            result.setMsg(null);
            result.setSuccess(true);
            result.setData(map);
        } else {
            map.put("status", false);
            result.setCode(0);
            result.setMsg(null);
            result.setSuccess(true);
            result.setData(map);
        }
        return result;
    }

    @GetMapping("/session")
    @SuppressWarnings("unchecked")
    @ApiOperation(value = "获取登录信息", notes = "获取用户的登录信息", code = 200, produces = "application/json", responseContainer = "Map", response = Result.class)
    @ApiImplicitParam(value = "用户Id", name = "userId", required = true, dataType = "string", paramType = "query")
    public Result getSession(@ApiParam(name = "userId", value = "用户Id", required = true) String userId) {
        log.info("/user/session");

        UserDomain userWithoutPassword = userService.getUserWithoutPasswordByLoginId(userId);

        result.setCode(0);
        result.setMsg(null);
        result.setSuccess(true);
        result.setData(userWithoutPassword);
        return result;
    }

    /**
     * 增加一个用户
     *
     * @param user user实体
     * @return status
     */
    @PostMapping("/add")
    @ApiOperation(value = "新增用户", notes = "添加一个新用户", code = 200, produces = "application/json", responseContainer = "List", response = Result.class)
    @ApiImplicitParams({
            @ApiImplicitParam(value = "用户账号", name = "userName", required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(value = "用户密码", name = "password", required = true, dataType = "string", paramType = "query")
    })
    public int addUser(UserDomain user) {
        log.info("/user/add");

        return userService.addUser(user);
    }

    /**
     * 删一个用户
     *
     * @param userId userId
     * @return status
     */
    @DeleteMapping("/delete")
    @ApiOperation(value = "删除用户", notes = "删除指定的用户", code = 200, produces = "application/json", responseContainer = "List", response = Result.class)
    @ApiImplicitParam(value = "用户Id", name = "userId", required = true, dataType = "string", paramType = "query")
    public int deleteUser(String userId) {
        log.info("/user/delete");

        return userService.deleteUser(userId);
    }

    /**
     * 更新密码
     *
     * @param userName userName
     * @param password password
     * @return status
     */
    @PutMapping("/update")
    @ApiOperation(value = "更新用户", notes = "更新指定的用户", code = 200, produces = "application/json", responseContainer = "List", response = Result.class)
    @ApiImplicitParams({
            @ApiImplicitParam(value = "用户账号", name = "userName", required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(value = "用户密码", name = "password", required = true, dataType = "string", paramType = "query")
    })
    public int updateUser(String userName, String password) {
        log.info("/user/update");

        return userService.updateUser(userName, password);
    }

    /**
     * 查一个用户
     *
     * @param userId 用户名
     * @return user实体
     */
    @GetMapping("/select")
    @ApiOperation(value = "查询用户", notes = "查询指定的用户", code = 200, produces = "application/json", responseContainer = "Map", response = Result.class)
    @ApiImplicitParam(value = "用户Id", name = "userId", required = true, dataType = "string", paramType = "query")
    public UserDomain getUser(String userId) {
        log.info("/user/select");

        return userService.getUser(userId);
    }

    /**
     * 查询所有用户信息
     *
     * @param pageNum  页码
     * @param pageSize 每页多少个
     * @return 所有用户信息
     */
    @SuppressWarnings("unchecked")
    @GetMapping("/all")
    @ApiOperation(value = "查询所有用户", notes = "查询所有的用户", code = 200, produces = "application/json", responseContainer = "List", response = Result.class)
    public Object findAllUser(@RequestParam(name = "pageNum", required = false, defaultValue = "1") int pageNum,
                              @RequestParam(name = "pageSize", required = false, defaultValue = "10") int pageSize,
                              @RequestParam(name = "id", required = false) String id) {
        log.info("/user/all");

        PageInfo<UserDomain> userDomainPageInfo;
        if (StringUtil.isNotEmpty(id)) {
            userDomainPageInfo = userService.findAllUser(pageNum, pageSize, id);
        } else {
            userDomainPageInfo = userService.findAllUser(pageNum, pageSize);
        }

        pageResult.setCode(0);
        pageResult.setMsg("请求成功");
        pageResult.setSuccess(true);
        pageResult.setCount(userDomainPageInfo.getTotal());
        pageResult.setData(userDomainPageInfo.getList());
        return pageResult;
    }
}